package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import Tools.MyPrintStream;
import controller.operator;

public class MyJFrame2 extends JFrame implements ActionListener
{
    final JTextArea showtext=new JTextArea();
	JPanel mainPanel;
	JPanel secPanel;
	JButton ok;
	JTextField text;
	JTextField textA;
	JTextField textB;
	JTextField textP;
	JTextField textPx;
	JTextField textPy;
	JTextField textSy;
	JLabel labelA;
	JLabel labelB;
	JLabel labelP;
	JLabel labelPx;
	JLabel labelPy;
	JLabel labelSy;
	
	JTextField user;
	JButton buttonAS;
	JTextField idtext;
	JPasswordField pwdtext;
	JPasswordField newpwdtext;
	JLabel idlabel;
	JLabel PWDlabel;
	JLabel NewPWDlabel;
	JButton buttonReg;
	JButton buttonLogin;
	JButton buttonChange;
	JButton buttonCer;
	JButton buttonUpd;
	JTextField textConn;
	JTextField textFromID;
	JTextField textTOID;
	JLabel labelFrom;
	JLabel labelTOID;
	JButton buttonConn;
	JButton buttonJuBao;
	JFrame showFrame;
	JFrame ASFrame;
	//椭圆参数设置窗体
	public class secJFrame extends JFrame implements ActionListener
	{
		public secJFrame()
		{
			//super();
			ASFrame =new JFrame("AS服务商初始化");
			ASFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			ASFrame.setTitle("设置椭圆曲线参数");
			ASFrame.setVisible(true);
			ASFrame.setResizable(false);
			ASFrame.setLayout(null);
			ASFrame.setBounds(500,100,400,180);
			ASFrame.getContentPane().setBackground(new Color(255,255,255));
		    
		    //“设置椭圆曲线参数”
		    text=new JTextField();
		    text.setFont(new Font("宋体",Font.BOLD,20));
		    text.setEditable(false);
		    text.setBounds(0, 5, 400, 25);
	        Font f = text.getFont();
	        FontMetrics fm = text.getFontMetrics(f);
	        int x = fm.stringWidth("设置椭圆曲线参数");
	        int y = fm.stringWidth(" ");
	        int z = text.getWidth()/2 - (x/2);
	        int w = z/y;
	        String pad ="";
	        pad = String.format("%"+w+"s", pad);
	        text.setText(pad+"设置椭圆曲线参数");
	        
	        //输入框a
	        textA=new JTextField();
	        textA.setBounds(0, 45, 120, 20);
	        
	        //输入框b
	        textB=new JTextField();
	        textB.setBounds(140, 45, 120, 20);
	        
	        //输入框p
	        textP=new JTextField();
	        textP.setBounds(280, 45, 120, 20);
	        
	        //输入框px
	        textPx=new JTextField();
	        textPx.setBounds(0, 80, 120, 20);
	        
	        //输入框py
	        textPy=new JTextField();
	        textPy.setBounds(140, 80, 120, 20);
	        
	        //输入框私钥
	        textSy=new JTextField();
	        textSy.setBounds(280, 80, 120, 20);
	        
	        //a
	        Font font=new Font("宋体",Font.PLAIN,15);
	        labelA=new JLabel("a");
	        labelA.setFont(font);
	        labelA.setBounds(57,30,20,15);
	        
	        //b
	        labelB=new JLabel("b");
	        labelB.setFont(font);
	        labelB.setBounds(197,30,20,15);
	        
	        //P
	        labelP=new JLabel("P");
	        labelP.setFont(font);
	        labelP.setBounds(337,30,20,15);
	        
	        //px
	        labelPx=new JLabel("Px");
	        labelPx.setFont(font);
	        labelPx.setBounds(57,65,20,15);
	        
	        //px
	        labelPy=new JLabel("Py");
	        labelPy.setFont(font);
	        labelPy.setBounds(197,65,20,15);
	        
	        //Sy
	        labelSy=new JLabel("私钥");
	        labelSy.setFont(font);
	        labelSy.setBounds(327,65,30,15);
	        //ok
	        ok=new JButton("完成设置");
	        ok.setBounds(0,105,400,30);
	        ok.addActionListener(this);
	        
	        //secPanel
		    secPanel=new JPanel();
		    secPanel.setOpaque(false);
		    secPanel.setVisible(true);
		    secPanel.setBounds(0,5,400,180);
		    secPanel.setLayout(null);
		    
		    secPanel.add(text);
		    secPanel.add(textA);
		    secPanel.add(textB);
		    secPanel.add(textP);
		    secPanel.add(textPx);
		    secPanel.add(textPy);
		    secPanel.add(textSy);
		    secPanel.add(labelA);
		    secPanel.add(labelB);
		    secPanel.add(labelP);
		    secPanel.add(labelPx);
		    secPanel.add(labelSy);
		    secPanel.add(labelPy);
		    secPanel.add(ok);
		    ASFrame.add(secPanel);
		   
		    ok.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					ASFrame.setVisible(false);
					showFrame.setVisible(true);
					//获取到参数
					String a=textA.getText();
					String b=textB.getText();
					String p=textP.getText();
					String px=textPx.getText();
					String py=textPy.getText();
					String x=textSy.getText();
					MyPrintStream mps = new MyPrintStream(System.out, showtext);  
					System.setOut(mps);  
					System.setErr(mps);
					String res="椭圆曲线的各个参数是：a="+a+",b="+b+",p="+p
							+",px="+px+",py="+py+",x="+x;
					System.out.println(res);
					System.out.println("椭圆曲线初始化状态:"+operator.ASinit(a, b, p, px, py, x));
					//其他的按钮弹对话框也可以用这个方法，第一个参数不用给，第二个参数是对话框内容，第三个参数是对话框标题，第四个参数是对话框类型。
					JOptionPane.showMessageDialog(null, "设置成功", "提示", JOptionPane.INFORMATION_MESSAGE); 
					
				}
			});
		   
		    
		    
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
		}
	}
    public MyJFrame2()
    {
    	super();
    	//子窗体
    	Toolkit kit=Toolkit.getDefaultToolkit();  
    	Dimension screenSize=kit.getScreenSize();      	  
    	int screenWidth=screenSize.width;     	  
    	int screenHeight=screenSize.height;  
    	showFrame=new JFrame();
    	showFrame.setBounds(50,50,(screenWidth*3)/4,(screenHeight*3)/4);
    	showFrame.setVisible(false);
    	showFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    	//JTextField showtext=new JTextField();
    	showtext.setBounds(10,20,(screenWidth*3)/4-40,(screenHeight*3)/4-80);
    	showtext.setEditable(false);
    	showtext.setLineWrap(true);
    	showtext.setFont(new Font("黑体", Font.BOLD, 15));
    	showtext.setCaretPosition(showtext.getText().length());
    	JScrollPane showpane=new JScrollPane(showtext);
    	showpane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    	showFrame.add(showpane);
    	
    	//主窗体
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(400,430);
		this.setLocation(100, 100);
	    this.setTitle("TEAM");
	    this.setVisible(true);
	    this.setResizable(false);
	    this.setLayout(null);
	    this.getContentPane().setBackground(new Color(255,255,255));
	    
	    //主面板
	    mainPanel=new JPanel();
	    mainPanel.setOpaque(false);
	    mainPanel.setVisible(true);
	    mainPanel.setBounds(0, 0, 400, 430);
	    mainPanel.setLayout(null);
	    
        //AS初始化....按钮
        buttonAS=new JButton("AS初始化，生成PSK集及公钥");
        buttonAS.setBounds(0,5,400,30);
        buttonAS.addActionListener(this);
        
        //idtext
        idtext=new JTextField();
        idtext.setText("ID可用字符:a-f,0-9");
        idtext.setBounds(160, 140, 160, 20);
        
        //pwdtext
        pwdtext=new JPasswordField();
        pwdtext.setBounds(160, 165, 160, 20);
        
        //newpwdtext
        newpwdtext=new JPasswordField();
        newpwdtext.setBounds(160, 190, 160, 20);
        
        //id
        idlabel=new JLabel("ID");
        idlabel.setBounds(145,140,20,20);
        
        //pwd
        PWDlabel=new JLabel("PWD");
        PWDlabel.setBounds(130,165,40,20);
        
        //newpwd
        NewPWDlabel=new JLabel("NewPWD");
        NewPWDlabel.setBounds(105,190,60,20);
        
        //注册
        buttonReg=new JButton("注册");
        buttonReg.setBounds(0,215,133,30);
        buttonReg.addActionListener(this);
        
        //登录
        buttonLogin=new JButton("登录");
        buttonLogin.setBounds(133,215,134,30);
        buttonLogin.addActionListener(this);
        
        //修改密码
        buttonChange=new JButton("修改密码");
        buttonChange.setBounds(267,215,133,30);
        buttonChange.addActionListener(this);
        
        //认证
        buttonCer=new JButton("认证");
        buttonCer.setBounds(0,245,200,30);
        buttonCer.addActionListener(this);
        
        //密钥更新
        buttonUpd=new JButton("密钥更新");
        buttonUpd.setBounds(200,245,200,30);
        buttonUpd.addActionListener(this);
        
        //安全通信
        textConn=new JTextField();
        textConn.setFont(new Font("宋体",Font.BOLD,20));
        textConn.setBounds(0,280,400,30);
        textConn.setEditable(false);
        Font f = textConn.getFont();
        FontMetrics fm = textConn.getFontMetrics(f);
        int x = fm.stringWidth("安全通信");
        int y = fm.stringWidth(" ");
        int z = textConn.getWidth()/2 - (x/2);
        int w = z/y;
        String pad="";
        pad = String.format("%"+w+"s", pad);
        textConn.setText(pad+"安全通信");
        
      	//user
        user=new JTextField();
        user.setBounds(0,50,400,40);
        user.setFont(new Font("宋体",Font.BOLD,20));
        user.setEditable(false);
        x=fm.stringWidth("用户操作");
        y=fm.stringWidth(" ");
        z=user.getWidth()/2-(x/2);
        w=z/y;
        pad="";
        pad=String.format("%"+w+"s", pad);
        user.setText(pad+"用户操作");
        
        //FromIDtext
        textFromID=new JTextField();
        textFromID.setBounds(160, 315, 160, 20);
        
        //TOIDtext
        textTOID=new JTextField();
        textTOID.setBounds(160,340,160,20);
        
        //From
        labelFrom=new JLabel("FromID");
        labelFrom.setBounds(120,315,40,20);
        
        //TOID
        labelTOID=new JLabel("TOID");
        labelTOID.setBounds(130, 340, 30, 20);
        
        //通信
        buttonConn=new JButton("通信");
        buttonConn.setBounds(0,365,200,30);
        buttonConn.addActionListener(this);
        
        //举报
        buttonJuBao=new JButton("举报请求通信车辆");
        buttonJuBao.setBounds(200,365,200,30);
        buttonJuBao.addActionListener(this);
        
        mainPanel.add(user);
	    mainPanel.add(buttonAS);
	    mainPanel.add(pwdtext);
	    mainPanel.add(newpwdtext);
	    mainPanel.add(idtext);
	    mainPanel.add(idlabel);
	    mainPanel.add(PWDlabel);
	    mainPanel.add(NewPWDlabel);
	    mainPanel.add(buttonChange);
	    mainPanel.add(buttonLogin);
	    mainPanel.add(buttonReg);
	    mainPanel.add(buttonUpd);
	    mainPanel.add(buttonCer);
	    mainPanel.add(textConn);
	    mainPanel.add(textTOID);
	    mainPanel.add(textFromID);
	    mainPanel.add(labelTOID);
	    mainPanel.add(labelFrom);
	    mainPanel.add(buttonJuBao);
	    mainPanel.add(buttonConn);
	    this.add(mainPanel);
	    
	    mainPanel.setSize(400, 429);
	    mainPanel.setSize(400, 430);
	    buttonAS.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				//弹出as初始化参数设置窗体
					new secJFrame();
				
			}
		});
	    
	    buttonReg.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				//点击按钮之后操作窗体自动隐藏，方便查看中间过程
				//mainframe.setExtendedState(JFrame.ICONIFIED);
				showFrame.setVisible(true);
				String ID=idtext.getText();
				String PWD=pwdtext.getText();
				System.out.println("ID="+ID+",PWD="+PWD);
				System.out.println("用户注册状态:"+operator.signIn(ID,PWD));
				MyPrintStream mps = new MyPrintStream(System.out, showtext);  
				System.setOut(mps);  
				System.setErr(mps);
				System.out.println("ID="+ID+",PWD="+PWD);
				//System.out.println("用户注册状态:"+operator.signIn(ID,PWD));
				int res=operator.signIn(ID,PWD);
				String message="null";
				if (res==1)
					message="ID为:"+ID+"的用户注册成功";
				else
					message="ID为:"+ID+"的用户注册失败";
				JOptionPane.showMessageDialog(null, message, "提示", JOptionPane.INFORMATION_MESSAGE); 
			}
		});
	    //为登录button添加监听事件
	    buttonLogin.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				MyPrintStream mps = new MyPrintStream(System.out, showtext);  
				System.setOut(mps);  
				System.setErr(mps);
				//点击按钮之后操作窗体自动隐藏，方便查看中间过程
				//mainframe.setExtendedState(JFrame.ICONIFIED);
				showFrame.setVisible(true);
				String ID=idtext.getText();
				String PWD=pwdtext.getText();
				System.out.println("ID="+ID+",PWD="+PWD);
				//System.out.println("用户登录状态值:"+operator.login(ID, PWD));
				int res=operator.login(ID, PWD);
				String message="null";
				if (res==1)
					message="ID为:"+ID+"的用户登录成功";
				else
					message="ID为:"+ID+"的用户登录失败";
				JOptionPane.showMessageDialog(null, message, "提示", JOptionPane.INFORMATION_MESSAGE); 
			}
		});
	    
	    //为修改密码button添加监听事件
	    buttonChange.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				MyPrintStream mps = new MyPrintStream(System.out, showtext);  
				System.setOut(mps);  
				System.setErr(mps);
				//点击按钮之后操作窗体自动隐藏，方便查看中间过程
				//mainframe.setExtendedState(JFrame.ICONIFIED);
				showFrame.setVisible(true);
				String ID=idtext.getText();
				String PWD=pwdtext.getText();
				String newPwd=newpwdtext.getText();
				System.out.println("ID="+ID+",PWD="+PWD+",newPwd"+newPwd);
				//System.out.println("用户修改密码状态值:"+operator.fixPassword(ID, PWD, newPwd));
				int res=operator.fixPassword(ID, PWD, newPwd);
				String message="null";
				if (res==1)
					message="ID为:"+ID+"的用户修改密码成功";
				else
					message="ID为:"+ID+"的用户修改密码失败";
				JOptionPane.showMessageDialog(null, message, "提示", JOptionPane.INFORMATION_MESSAGE); 
			}
		});
	    //为认证按钮添加监听事件
	    buttonCer.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				MyPrintStream mps = new MyPrintStream(System.out, showtext);  
				System.setOut(mps);  
				System.setErr(mps);
				//点击按钮之后操作窗体自动隐藏，方便查看中间过程
				//mainframe.setExtendedState(JFrame.ICONIFIED);
				showFrame.setVisible(true);
				String ID=idtext.getText();
				System.out.println("ID="+ID);
				String psk=operator.getCheck(ID);
				System.out.println("用户认证获得的psk为:"+psk);
				JOptionPane.showMessageDialog(null, "获取psk操作完成", "提示", JOptionPane.INFORMATION_MESSAGE); 
			}
		});
	    
	    //为密钥更新按钮添加事件监听
	    buttonUpd.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				MyPrintStream mps = new MyPrintStream(System.out, showtext);  
				System.setOut(mps);  
				System.setErr(mps);
				//点击按钮之后操作窗体自动隐藏，方便查看中间过程
				//mainframe.setExtendedState(JFrame.ICONIFIED);
				showFrame.setVisible(true);
				String ID=idtext.getText();
				System.out.println("ID="+ID);
				System.out.println("用户获得的新密钥是:"+operator.updatePsk(ID));
				JOptionPane.showMessageDialog(null, "更新psk操作完成", "提示", JOptionPane.INFORMATION_MESSAGE); 
			}
		});
	    
	    //为通信按钮添加事件监听
	    buttonConn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				MyPrintStream mps = new MyPrintStream(System.out, showtext);  
				System.setOut(mps);  
				System.setErr(mps);
				//点击按钮之后操作窗体自动隐藏，方便查看中间过程
				//mainframe.setExtendedState(JFrame.ICONIFIED);
				showFrame.setVisible(true);
				String fromID=textFromID.getText();
				String toID=textTOID.getText();
				System.out.println("fromID="+fromID+",toID="+toID);
				System.out.println("通信双方获取的通信密钥是:"+operator.safeCommunicate(fromID, toID));
				JOptionPane.showMessageDialog(null, "通信车辆已获取通信秘钥", "提示", JOptionPane.INFORMATION_MESSAGE); 
			}
		});
	    
	    //为举报按钮添加事件监听
	    buttonJuBao.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				MyPrintStream mps = new MyPrintStream(System.out, showtext);  
				System.setOut(mps);  
				System.setErr(mps);
				//点击按钮之后操作窗体自动隐藏，方便查看中间过程
				//mainframe.setExtendedState(JFrame.ICONIFIED);
				showFrame.setVisible(true);
				String evilID=textFromID.getText();
				System.out.println("evilID="+evilID);
				String ID=operator.report(evilID);
				System.out.println("被举报的车辆是:"+ID);
				String message="被举报车辆ID为:"+ID;
				JOptionPane.showMessageDialog(null, message, "提示", JOptionPane.INFORMATION_MESSAGE); 
				
			}
		});
	   
    }
	@Override
	public void actionPerformed(ActionEvent arg0) {

		
	}
	
	public static void main(String[] args) {
		new MyJFrame2();
	}
}
